import redis
from django.conf import settings


class RedisClient:
	def __init__(self):
		self.client = redis.Redis(
			host=settings.REDIS_HOST,
			port=settings.REDIS_PORT,
			db=settings.REDIS_DB,
			password=settings.REDIS_PASSWORD,
			decode_responses=True
		)

	def set(self, key, value, expire=None):
		"""设置键值对，可选过期时间（秒）"""
		return self.client.set(key, value, ex=expire)

	def get(self, key):
		"""获取键的值"""
		return self.client.get(key)

	def delete(self, key):
		"""删除键"""
		return self.client.delete(key)

	def exists(self, key):
		"""判断键是否存在"""
		return self.client.exists(key)


# 创建Redis客户端实例
redis_client = RedisClient()
